//
//  NetworkManager.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/23.
//

import Foundation
import Moya

class JPushManager {
    static let shared = JPushManager()
    
    private let provider: MoyaProvider<JPushApi>
    
    private init() {
        provider = MoyaProvider<JPushApi>()
    }
    //统一请求处理
    private func request<M: Codable>(_ target: JPushApi, modeType: M.Type) async throws-> M {
        try await withCheckedThrowingContinuation({ continuation in
            provider.request(target) { result in
                //调用统一接口处理
                dealNetResult(result: result, modeType: modeType) {
                    continuation.resume(returning: $0)
                } fail: {
                    continuation.resume(throwing: $0)
                }
            }
        })
    }
}

//接口请求
extension JPushManager {
    func getExample<M: Codable>(modeType: M.Type) async throws-> M {
        let api : JPushApi = .getExample
        return try await request(api, modeType: modeType)
    }
    
    func postFormExample<M: Codable>(formData: [String: Any], modeType: M.Type) async throws-> M {
        let api : JPushApi = .postFormExample(formData: formData)
        return try await request(api, modeType: modeType)
    }
    
    //极光推送
    func pushJg<M: Codable>(jsonObject: String, modeType: M.Type) async throws-> M {
        let api : JPushApi = .pushJg(token: "YzlhMDRkM2FjZDBmNjZkYjZhNjZhYjNmOmU0OWVmNGUwNGEzZDU4ZWEyYTY5MjkxNA==", jsonString: jsonObject)
        return try await request(api, modeType: modeType)
    }
}
